package ru.tomsk.pear.remotecontrolclient;

import java.io.PrintWriter;
import java.net.Socket;
import java.io.*;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class RemoteControlClient extends Activity {
	private Button volumeUp, volumeDown, mute, unmute;
    private String serverIpAddress = "192.168.10.1";
    private TextView label;
    //private Handler handler = new Handler();
    boolean connected = false;
    int volume = 30000;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        volumeUp = (Button) findViewById(R.id.volumeUp);
        volumeDown = (Button) findViewById(R.id.volumeDown);
        mute = (Button) findViewById(R.id.mute);
        unmute = (Button) findViewById(R.id.unmute);
        label = (TextView) findViewById(R.id.str);
        label.setText("This is the remote control!");        
        volumeUp.setOnClickListener(
    		new OnClickListener() {
    	        @Override
    	        public void onClick(View v) {
    	        	Log.d("ClientActivity", "volumeUp");
    	            if (!connected) {
    	            	label.setText("You click to volumeUp");
    	            	volume += 1000;
    	                Thread cThread = new ClientThread("pactl set-sink-volume '0' " + volume);
    	            	// Thread cThread = new ClientThread("gedit");
    	                cThread.start();
    	            }
    	        }
        	}
        );
        
        volumeDown.setOnClickListener(
        		new OnClickListener() {
        	        @Override
        	        public void onClick(View v) {
        	        	Log.d("ClientActivity", "volumeDown");
        	            if (!connected) {
        	            	label.setText("You click to volumeDown");
        	            	volume -= 1000;
        	                Thread cThread = new ClientThread("pactl set-sink-volume '0' " + volume);
        	                cThread.start();
        	            }
        	        }
            	}
            );
        
        mute.setOnClickListener(
        		new OnClickListener() {
        	        @Override
        	        public void onClick(View v) {
        	        	Log.d("ClientActivity", "mute");
        	            if (!connected) {
        	            	label.setText("You click to mute");
        	                Thread cThread = new ClientThread("pactl set-sink-mute '0' true");
        	                cThread.start();
        	            }
        	        }
            	}
            );
        
        unmute.setOnClickListener(
        		new OnClickListener() {
        	        @Override
        	        public void onClick(View v) {
        	        	Log.d("ClientActivity", "unmute");
        	            if (!connected) {
        	            	label.setText("You click to unmute");
        	                Thread cThread = new ClientThread("pactl set-sink-mute '0' false");
        	                cThread.start();
        	            }
        	        }
            	}
            );
        
        
        
        
        Log.d("ClientActivity", "Constructor done!");
    }
    
/*    private OnClickListener connectListener = 
    	new OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	Log.d("ClientActivity", "unmute");
	            if (!connected) {
	            	label.setText("You click to unmute");
	                Thread cThread = new ClientThread("");
	                cThread.start();
	            }
	        }
    	}
    */
    /*public class ClientThread implements Runnable {

        public void run() {
        	String str = "/home/akato/1";
        	int i = 10;
            try {
            	Log.d("ClientActivity Thread", "C: Connecting...");
                Socket socket = new Socket(serverIpAddress, 5638);
                PrintWriter out =  new PrintWriter(socket.getOutputStream(), true);
                connected = true;
                while (connected && i > 0) {
                   // try {           
                    	Log.d("ClientActivity Thread", "C: Sending command.");
                        
                        //new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                        out.println(str);
                        Log.d("ClientActivity Thread", "C: Sent.");
                       
                    //} catch (UnhandledException e) {
                    	//Log.e("ClientActivity Thread", "S: Error while send message");
                    //}
                    i--;
                }
                socket.close();
                Log.d("ClientActivity Thread", "C: Closed.");
            } catch (IOException e) {
            	Log.e("ClientActivity", "C: WTF error?", e);
                connected = false;
            }
        }
    }*/
    public class ClientThread extends Thread {
    	
    	private String message; 
    	public ClientThread(String msg) {
    		super();
    		message = msg;
    	}

        public void run() {
            try {
            	Log.d("ClientActivity Thread", "C: Connecting...");
                Socket socket = new Socket(serverIpAddress, 5638);
                PrintWriter out =  new PrintWriter(socket.getOutputStream(), true);
            	Log.d("ClientActivity Thread", "C: Sending command.");
            	out.println(message);
                Log.d("ClientActivity Thread", "C: Sent."); 
                socket.close();
                try {
                	this.join();
                } catch (InterruptedException e) {
                	Log.e("ClientActivity Thread", "C: Can't join thread", e);
                }
                Log.d("ClientActivity Thread", "C: Closed.");
            } catch (IOException e) {
            	Log.e("ClientActivity Thread", "C: IO error", e);
            }
        }
    }

}

